#include <gflags/gflags.h>

#include "mysql_friend_event.h"
#include "mysql_relationship.h"
#include "mysql_session_info.h"

#include "log.h"

DEFINE_bool(run_mode, false, "程序的运行模式，false-调试； true-发布；");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

void r_insert_test(chat::RelationshipTable &tb) {
    tb.insert("用户ID1", "用户ID2");
    tb.insert("用户ID1", "用户ID3");
}
void r_select_test(chat::RelationshipTable &tb) {
    auto res = tb.friends("用户ID1");
    for (auto &uid : res) {
        std::cout << uid << std::endl;
    }
}
void r_remove_test(chat::RelationshipTable &tb) {
    tb.remove("用户ID2", "用户ID1");
}

void r_exists_test(chat::RelationshipTable &tb) {
    std::cout << tb.exist("用户ID2", "用户ID1") << std::endl;
    std::cout << tb.exist("用户ID3", "用户ID1") << std::endl;
}

void a_insert_test(chat::FriendEventTable &tb) {
    chat::FriendEvent fa1("uuid1", "用户ID1", "用户ID2");
    tb.insert(fa1);

    chat::FriendEvent fa2("uuid2", "用户ID1", "用户ID3");
    tb.insert(fa2);

    chat::FriendEvent fa3("uuid3", "用户ID2", "用户ID3");
    tb.insert(fa3);
}
void a_remove_test(chat::FriendEventTable &tb) {
    tb.remove("用户ID1", "用户ID2");
}

void a_select_test(chat::FriendEventTable &tb) {
    // chat::FriendEvent fa3("uuid3", "用户ID2", "用户ID3");
    // tb.insert(fa3);

    auto res = tb.perrs("用户ID3");
    for (auto &uid : res) {
        std::cout << uid << std::endl;
    }
}
void a_exists_test(chat::FriendEventTable &tb) {
    std::cout << tb.exists("用户ID1", "用户ID2")
              << std::endl;
    std::cout << tb.exists("用户ID1", "用户ID3")
              << std::endl;
    std::cout << tb.exists("用户ID2", "用户ID3")
              << std::endl;
}

void c_insert_test(chat::ChatSessionInfoTable &tb) {
    chat::ChatSessionInfo cs1("会话ID1", "会话名称1",
                              (unsigned char)chat::ChatSessionType::SINGLE);
    tb.insert(cs1);
    chat::ChatSessionInfo cs2("会话ID2", "会话名称2",
                              (unsigned char)chat::ChatSessionType::GROUP);
    tb.insert(cs2);
    chat::ChatSessionInfo cs3("会话ID3", "会话名称3",
                              (unsigned char)chat::ChatSessionType::SINGLE);
    tb.insert(cs3);
}

void c_select_test(chat::ChatSessionInfoTable &tb) {
    auto res = tb.sessionInfo("会话ID1");
    std::cout << res.chat_session_id() << std::endl;
    std::cout << res.chat_session_name() << std::endl;
    std::cout << (int)res.chat_session_type() << std::endl;
}

void c_single_test(chat::ChatSessionInfoTable &tb) {
    auto res = tb.SingleSessionInfos("用户ID1");
    for (auto &info : res) {
        std::cout << info.chat_session_id << std::endl;
        std::cout << info.peer_id << std::endl;
    }
}
void c_group_test(chat::ChatSessionInfoTable &tb) {
    auto res = tb.groupSessionInfos("用户ID1");
    for (auto &info : res) {
        std::cout << info.chat_session_id << std::endl;
        std::cout << info.chat_session_name << std::endl;
    }
}
void c_remove_test(chat::ChatSessionInfoTable &tb) { tb.remove("会话ID3"); }
void c_remove_test2(chat::ChatSessionInfoTable &tb) {
    tb.remove("用户ID1", "用户ID2");
}

int main(int argc, char *argv[]) {
    google::ParseCommandLineFlags(&argc, &argv, true);
    chat::initLogger(FLAGS_log_level, FLAGS_run_mode, FLAGS_log_file);

    auto db = chat::DataBaseFactory::create(1, "root", "123", "chat",
                                            "127.0.0.1", 0, "utf8");
    chat::RelationshipTable rtb(db);
    chat::FriendEventTable fatb(db);
    chat::ChatSessionInfoTable cstb(db);

    // r_insert_test(rtb);
    // r_select_test(rtb);
    // r_remove_test(rtb);
    // r_exists_test(rtb);
    // a_insert_test(fatb);
    // a_remove_test(fatb);
    // a_select_test(fatb);
    // a_exists_test(fatb);
    // c_insert_test(cstb);
    // c_select_test(cstb);
    // c_single_test(cstb);
    // std::cout << "--------------\n";
    // c_group_test(cstb);
    // c_remove_test(cstb);
    c_remove_test2(cstb);
    return 0;
}